package com.neusoft.elm_servlet.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.elm_servlet.po.Cart;
import com.neusoft.elm_servlet.service.CartService;
import com.neusoft.elm_servlet.service.impl.CartServiceImpl;

public class CartController {

	CartService cardService = new CartServiceImpl();
	
	public List<Cart> listCart(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		//获取参数
		String userId = request.getParameter("userId");
		String businessId = request.getParameter("businessId");
		//将参数传给对象调用方法，获取返回值
		List<Cart> list = cardService.listCart(userId,businessId);
		return list;
	}
	
	public int saveCart(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		//获取参数
		String userId = request.getParameter("userId");
		String businessId = request.getParameter("businessId");
		String foodId = request.getParameter("foodId");
		
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(Integer.valueOf(businessId));
		cart.setFoodId(Integer.valueOf(foodId));
		
		int count = cardService.saveCart(cart);
		return count;
	}
	
	public int updateCart(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		//获取参数
		String userId = request.getParameter("userId");
		String businessId = request.getParameter("businessId");
		String foodId = request.getParameter("foodId");
		String quantity = request.getParameter("quantity");
		
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(Integer.valueOf(businessId));
		cart.setFoodId(Integer.valueOf(foodId));
		cart.setQuantity(Integer.valueOf(quantity));
		
		int count = cardService.updateCart(cart);
		return count;
	}
	
	public int removeCart(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		//获取参数
		String userId = request.getParameter("userId");
		String businessId = request.getParameter("businessId");
		String foodId = request.getParameter("foodId");
		
		Cart cart = new Cart();
		cart.setUserId(userId);
		cart.setBusinessId(Integer.valueOf(businessId));
		if (foodId != null && foodId !="") {
			cart.setFoodId(Integer.valueOf(foodId));
		}
		int count = cardService.removeCart(cart);
		return count;
	}
}
